﻿--[[
   @brief    descprtion
   @author   vincentlao
   @version  1.0
   @date     2015-06-10
--]]
talk_dialog_control = 
{
    to_create_session_group = {};
    to_create_session_friend = {};
    inited = false;
    is_get_nameplate_pk_history = false;
    FRIEND_FLAG = 1;
    GROUP_FLAG = 2;
    current_session_group = {};
    current_session_friend = {};
    active_session_id = nil;
    active_session_flag = 0;
    is_window_active = false;
    session_history_msg = {};
    ignore_restore = false;
    NEW_GROUP_NEW_MEMBER_MSG_TYPE = 0x81;
    NEW_GROUP_QUIT_MEMBER_MSG_TYPE = 0xd;
    NEW_GROUP_REMOVE_MEMBER_MSG_TYPE = 0xe;     
    NEW_GROUP_SYS_REPORT = 0xff;      --系统通知
    ready_to_show_list = {};
    

    init = function()
        base_utility.log("talk_dialog init");
        ierd_tgp.tpf_ui.Require("social\\talk_dialog\\view.lua");  
        ierd_tgp.tpf_ui.Require("social\\talk_dialog\\model.lua");
        ierd_tgp.tpf_ui.Require("social\\group_message\\model.lua");
        ierd_tgp.tpf_ui.Require("social\\group_list\\control.lua");
        ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");
        ierd_tgp.tpf_ui.Require("social\\friend_message\\model.lua");
        ierd_tgp.tpf_ui.Require("social\\friend_list\\control.lua");
        ierd_tgp.tpf_ui.Require("social\\friend_list\\model.lua");
        ierd_tgp.tpf_ui.Require("social\\group_menu\\control.lua");
        ierd_tgp.tpf_ui.Require("social\\control.lua");  

        ierd_tgp.tpf_ui.Require("social\\talk_dialog\\live_room_view.lua");  
        ierd_tgp.tpf_ui.Require("social\\talk_dialog\\live_room_control.lua");  
        ierd_tgp.tpf_ui.Require("social\\talk_dialog\\live_room_model.lua");  

        talk_dialog_control.init_func();

        comm_center.listen_broadcast("i_broadcast_del_friend", talk_dialog_control.on_broadcast_del_friend);
        comm_center.reg_service('i_service_open_session', talk_dialog_control.service_open_session);
        comm_center.listen_broadcast('i_broadcast_update_friend_online_status', talk_dialog_control.on_broadcast_update_friend_online_status); -- 是新IM更新游戏状态的
    end;

    init_func = function()
        talk_dialog_control.to_create_session_group = {};
        talk_dialog_control.to_create_session_friend = {};
        talk_dialog_control.FRIEND_FLAG = 1;
        talk_dialog_control.GROUP_FLAG = 2;
        talk_dialog_control.current_session_group = {};
        talk_dialog_control.current_session_friend = {};
        talk_dialog_control.active_session_id = nil;
        talk_dialog_control.active_session_flag = 0;
        talk_dialog_control.ready_to_show_list = {};
    end;


    show = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str) or {};
        local groups = table_param.groups;
        local friends = table_param.friends;

        local check_state = comm_center.call_service("i_service_check_pwg_social_state", {});
        if nil ~= check_state and true == check_state.is_enabled then
            local friend_id = "";
            if nil ~= friends and #friends > 0 then
                friend_id = tostring(friends[1].id);
            end
            local param = {
                session_type = 1;
                target_id = friend_id;
            };
            comm_center.call_service("i_service_new_social_open_panel", param);
            return;
        end
        -- 顶号提示窗口
        local dlg = ui_utility.get_ctrl_ptr_by_cid(CID_LOGIN_KICK_OFF_DLG);
        if dlg ~= nil then
            base_utility.log("[talk_dialog_control] kick off dlg is exist", true);
            return;
        end

        base_utility.log(string.format("[talk_dialog_control]show, json_str = %s", json_str));

        talk_dialog_control.ignore_restore = true;
        talk_dialog_view:show(true);
        talk_dialog_control.ignore_restore = false;

        

        if talk_dialog_control.inited == true then
            base_utility.log(string.format("[talk_dialog_control]show, inited = true"));
            talk_dialog_control.on_create_talk_session(groups, friends);
        else
            --begin：web没初始化，把参数缓存起来做重放（解决快速点击需要按顺序一次打开会话窗口的问题）
            table.insert(talk_dialog_control.ready_to_show_list, json_str);
            --end

            --talk_dialog_control.init_func();
            --[[local component = ui_utility.get_ctrl_ptr_by_cid(CID_WEB_CTRL_FOR_TALK_DIALOG);
            if component ~= nil then
                local on_restore_func, str_holder1 = U8toCUIStr("talk_dialog_control.on_restore");
                component:SetScriptFunction(Tenio.ESE_ON_DIALOG_RESTORE, on_restore_func);
            end

            if talk_dialog_control.to_create_session_group == nil then
                talk_dialog_control.to_create_session_group = {};
            end

            if groups ~= nil then
                for group_index = 1, #groups do
                    if talk_dialog_control.is_session_exist(talk_dialog_control.to_create_session_group, groups[group_index].id) == false then
                        table.insert(talk_dialog_control.to_create_session_group, groups[group_index]);
                    end
                end
            end

            if friends ~= nil then
                for friend_index = 1, #friends do
                    if talk_dialog_control.is_session_exist(talk_dialog_control.to_create_session_friend, friends[friend_index].id) == false then
                        table.insert(talk_dialog_control.to_create_session_friend, friends[friend_index]);
                    end
                end
            end--]]            
        end

        if talk_dialog_control.is_get_nameplate_pk_history == false then
            talk_dialog_control.get_history_pk_result();
        end
    end;

    on_js_callback = function(uilib, func_name, json)
        if json == nil or json == "" or func_name == nil or func_name == "" then
            return;
        end
        base_utility.log(string.format("[talk_dialog_control]on_js_callback, func_name = %s, json = %s", func_name, json));
        local table_param = base_utility.json_to_table(json);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        
        if func_name == "close" then
            talk_dialog_control.on_dialog_close_web();
            talk_dialog_view:hide();
            event_center.send_event(social_event.TALK_ACTIV_SESSION_CHANGED, base_utility.table_to_json({id = "", unread_count = 0}));
        elseif func_name == "web_inited" then
            talk_dialog_control.on_inited();
        elseif func_name == "web_is_live" then
            base_utility.remove_interval("talk_dialog_control.on_close_dailog_timer");
        elseif func_name == "get_my_info" then
            talk_dialog_control.on_get_my_info();
        elseif func_name == "resize" then
            talk_dialog_view:resize(table_param.width, table_param.height);
        elseif func_name == "get_group_info" then
            talk_dialog_control.on_get_group_info(table_param.group_id);
        elseif func_name == "send_msg_to_group" then
            --social_network:set_clipboard_text("ddad");
            social_network:send_msg_to_group(table_param.group_id, table_param.msg);
        elseif func_name == "modify_group_notify" then
            social_network:modify_group_notify(table_param.group_id, table_param.notify);
        elseif func_name == "get_group_all_message" then
            talk_dialog_control.on_get_group_all_message(table_param.group_id);
        elseif func_name == "get_group_unread_message_count" then
            talk_dialog_control.on_get_group_unread_message_count(table_param.group_id);
        elseif func_name == "get_friend_info" then
            talk_dialog_control.on_get_friend_info(table_param.sender);            
        elseif func_name == "send_msg_to_friend" then
            social_network:send_msg_to_friend(table_param.friend_id, table_param.msg);            
        elseif func_name == "get_friend_all_message" then
            talk_dialog_control.on_get_friend_all_message(table_param.friend_id);
        elseif func_name == "get_friend_unread_message_count" then
            talk_dialog_control.on_get_friend_unread_message_count(table_param.friend_id);       
        elseif func_name == "get_group_member" then
            social_network:get_group_member(table_param.group_id);     
        elseif func_name == "get_user_list_info" then
            social_network:get_friend_list_detail(json); 
        elseif func_name == "close_sessions" then
            talk_dialog_control.on_close_sessions(json);   
        elseif func_name == "set_group_admin" then
            social_network:set_group_admin(table_param.action, table_param.group_id, table_param.applicate);  
        elseif func_name == "kickout_group_member" then
            social_network:kickout_group_member(table_param.group_id, table_param.member); 
        elseif func_name == "show_personal_center" then
            local param = {friend_id = table_param.friend_id};
            if table_param.param then
                param["param"] = table_param.param;
            end
            event_center.send_event(group_event.SHOW_PERSONAL_CENTER,base_utility.table_to_json(param));
        elseif func_name == "show_group_center" then
            local param = {group_id = table_param.group_id}
            event_center.send_event(group_event.SHOW_GROUP_CENTER,base_utility.table_to_json(param));
        elseif func_name == "open_lol_room" then
            social_network:open_lol_chatroom(table_param.zone_id, table_param.group_id);
        elseif func_name == "join_lol_chatroom" then
            local tcls_id = social_network_model.tcls_zoen_id_from_lol_zone_id(table_param.zone_id);
            social_network:join_lol_chatroom(tcls_id, table_param.channel_id, table_param.group_id, table_param.psw, table_param.room_id, table_param.invitor_tgp_id);
        elseif func_name == "get_my_game_zone_info" then
            talk_dialog_control.get_my_game_zone_info(table_param.game_id);
        elseif func_name == "get_game_list" then
            social_network:get_game_list(table_param.friend_id, table_param.count);
        elseif func_name == "web_log" then
            talk_dialog_control.web_log(table_param.str_log);
        elseif func_name == "get_history_msg" then
            -- talk_dialog_control.get_history_msg(table_param.id, table_param.derection, table_param.offset, table_param.count);
        elseif func_name == "mask_group_msg" then
            group_menu_control.mask_group_message(table_param.mask_flag, table_param.group_id);
        elseif func_name == "quit_group" then
            social_network:quit_group(table_param.group_id, table_param.quiter);
        elseif func_name == "open_img_browse_floder" then
            --social_network:paste_pic(table_param.id, table_param.no);
            talk_dialog_view:set_focus_dialog();
            social_network:open_img_browse_floder(talk_dialog_view:get_window(), table_param.id, table_param.no);
            --social_network:show_pic(talk_dialog_view:get_window(),"http://img.blog.csdn.net/20150123104005592?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWZjaW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center", "file_path");
        elseif func_name == "paste_pic" then
            social_network:paste_pic(table_param.id, table_param.no);
        elseif func_name == "upload_pic" then
            social_network:upload_pic(table_param.file_path, table_param.md5, table_param.type);
        elseif func_name == "show_pic" then
            social_network:show_pic(talk_dialog_view:get_window(), table_param.url, table_param.file);
        elseif func_name == "have_pk_result" then
            talk_dialog_control.have_pk_result(table_param.group_id);  
        elseif func_name == "get_group_pk_result_detail" then
            talk_dialog_control.get_group_pk_result_detail(table_param.group_id);
        elseif func_name == "set_nameplate" then
            base_utility.log("[talk_dialog_control] web set nameplate, group_id:"..table_param.group_id..",nameplate_id:"..tostring(table_param.nameplate_id), true);
            talk_dialog_control.set_nameplate(table_param.group_id, table_param.nameplate_id);
        elseif func_name == "show_talk_dialog" then
            talk_dialog_control.show_talk_dialog(table_param.friend_id);
        elseif func_name == "add_friend" then
            talk_dialog_control.add_friend(table_param.friend_id);
        elseif func_name == "is_my_friend" then
            talk_dialog_control.is_my_friend(table_param.friend_id);
        elseif func_name == "remove_member" then
            talk_dialog_control.remove_member(table_param.group_id, json);
        elseif func_name == "set_manager" then
            talk_dialog_control.set_manager(group_id, json);
        elseif func_name == "cancel_manager" then
            talk_dialog_control.cancel_manager(group_id, json);
        elseif func_name == "set_clipboard_text" then
            social_network:set_clipboard_text(table_param.text);
        elseif func_name == "get_group_pre_page_message" or func_name == "get_group_pre_page_messag" then
            social_network:get_group_msgs(table_param.group_id, 20); 
        elseif func_name == "get_friend_pre_page_message" then
            social_network:get_friend_msgs(table_param.friend_id, 20);
        elseif func_name == "get_lol_last_zone" then
            talk_dialog_control.get_lol_last_zone(table_param.group_id);
        elseif func_name == "invite_lol_kaihei" then
            talk_dialog_control.invite_lol_kaihei(table_param.friend_id, table_param.nick_name);
        elseif func_name == "get_current_lol_zone_id" then
            talk_dialog_control.get_current_lol_zone_id(table_param.game_id, table_param.customer_id);
        elseif func_name == "show_create_discussion_dialog" then
            talk_dialog_control.show_create_discussion_dialog(json);
        elseif func_name == "edit_discussion_name" then
            talk_dialog_control.edit_discussion_name(json);
        elseif func_name == "get_friend_game_state" then
            talk_dialog_control.get_friend_game_state(json);
        elseif func_name == "set_lol_gaming_user_list" then
            talk_dialog_control.set_lol_gaming_user_list(json);
        elseif func_name == "get_game_name" then
            talk_dialog_control.get_game_names(json);
        elseif func_name == "set_game_invitation_result" then
            talk_dialog_control.set_game_invitation_result(json)
        elseif func_name == "get_game_base_info" then
            talk_dialog_control.get_game_base_info(json);
        end
    end;
    
    web_log = function(str_log)
        if str_log ~= nil then
            base_utility.log(string.format("[talk_dialog_control_web_log] %s", str_log));
        end
    end;

    get_lol_last_zone = function(group_id)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            ierd_tgp.tpf_ui.Require("social\\model.lua");
            local zone_id = social_network_model.get_lol_zone_id(); --lol
            local param_table = {};
            param_table["data"] = {};
            param_table["data"]["zone_id"] = zone_id;
            param_table["data"]["group_id"] = group_id;
            param_table["retCode"] = 0;
           web_utility.lua_call_js("get_lol_last_zone",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        end;
    end;

    on_send_msg_to_group = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_send_msg_to_group, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table["result"];
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table["group_id"];

       web_utility.lua_call_js("send_msg_to_group",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 


        if result_table.result == 0 then
            group_list_control.on_send_msg_to_group(result_table);
            local messages = talk_dialog_control.session_history_msg[result_table.group_id];
            if messages == nil then
                talk_dialog_control.session_history_msg[result_table.group_id] = {};
                messages = talk_dialog_control.session_history_msg[result_table.group_id];
            end

            talk_dialog_control.insert_msg_order(messages, result_table);
        end            
    end;

    on_modify_group_notify = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_modify_group_notify, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table["result"];
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table["group_id"];
        param_table["data"]["notification"] = result_table["notification"];

        if result_table.result == 0 then
            group_list_control.update_group_notice(result_table["group_id"], result_table["notification"]);
        end

       web_utility.lua_call_js("modify_group_notify",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    on_get_group_info  = function(group_id)
        local group_info = group_list_model.get_group_info(group_id);
        local param_table = {};

        if group_info == nil then 
            param_table["retCode"] = 1;
            param_table["msg"] = "fail";
            param_table["data"] = {};
           web_utility.lua_call_js("get_group_info",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
            return;
        end

        local game_id = 0;
        if group_info.ext_data2 ~= nil and group_info.ext_data2 ~= '' then
            local game_info_tbl = base_utility.json_to_table(group_info.ext_data2);
            if game_info_tbl ~= nil and game_info_tbl.game ~= nil then
                game_id = tonumber(game_info_tbl.game);
            end
        end
   
        local game_url = nil;

        if game_id ~= nil then
            local game_info = banner_model.get_game_base_info(game_id);
            if game_info ~= nil then
                game_url = game_info.icon_url;  
            end     
        end

        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = group_info;
        param_table["data"]["game_url"] = game_url;

       web_utility.lua_call_js("get_group_info",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);


        local param = {};
        param["group_id"] = group_id;
        event_center.send_event(group_event.OPEN_GROUP,base_utility.table_to_json(param));
    end;

    on_inited = function()
        talk_dialog_control.inited = true;
        if #talk_dialog_control.ready_to_show_list > 0 then
            for i=1,#talk_dialog_control.ready_to_show_list do
                talk_dialog_control.show(0, talk_dialog_control.ready_to_show_list[i]);
            end
        else
            return;
        end

        --[[if talk_dialog_control.to_create_session_group.count == 0  and talk_dialog_control.to_create_session_friend.count == 0 then
            return;
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        local group_table = {};
        local discussion_table = {};

        talk_dialog_control.current_session_group = {};
        if #talk_dialog_control.to_create_session_group > 0 then
            for group_index = 1, #talk_dialog_control.to_create_session_group do
                if "Public" == talk_dialog_control.to_create_session_group[group_index].group_type then
                    table.insert(group_table, talk_dialog_control.to_create_session_group[group_index]);
                    table.insert(talk_dialog_control.current_session_group, talk_dialog_control.to_create_session_group[group_index]);
                elseif "MobileDiscus" == talk_dialog_control.to_create_session_group[group_index].group_type then
                    table.insert(discussion_table, talk_dialog_control.to_create_session_group[group_index]);
                    table.insert(talk_dialog_control.current_session_group, talk_dialog_control.to_create_session_group[group_index]);
                end
            end
            param_table["data"]["groups"] = group_table;
            param_table["data"]["discussion"] = discussion_table;
        end

        if #talk_dialog_control.to_create_session_friend > 0 then
            param_table["data"]["friends"] = talk_dialog_control.to_create_session_friend;
        end

        base_utility.log(string.format("[talk_dialog_control]on_inited, json_str = %s",base_utility.table_to_json(param_table)));
        web_utility.lua_call_js("create_talk_session",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);

        talk_dialog_control.current_session_friend = talk_dialog_control.to_create_session_friend;  

        talk_dialog_control.to_create_session_group = {};
        talk_dialog_control.to_create_session_friend = {};]]--
    end;

    on_get_my_info = function()
        local user_game_info = ierd_tgp.users_info.User_game_info:new();
        local users_info_adapter =
            base_utility.get_component_adapter("IUsers_info_module",
                "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
        users_info_adapter:get_self_game_info(user_game_info);

        if login_model.im_login_infomation == nil then
            return;
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["id"] = login_model.im_login_infomation.friend_id;
        param_table["data"]["my_nick"] = login_model.im_login_infomation.name;
        param_table["data"]["head_url"] = login_model.im_login_infomation.url;
        param_table["data"]["remark"] = login_model.im_login_infomation.remark;
        param_table["data"]["level"] = login_model.im_login_infomation.level;
        param_table["data"]["sex"] = login_model.im_login_infomation.sex;    
        param_table["data"]["exp"] = login_model.im_login_infomation.exp;
        param_table["data"]["reg_time"] = login_model.im_login_infomation.reg_time; 
        param_table["data"]["last_login_time"] = login_model.im_login_infomation.last_login_time;
        param_table["data"]["status"] = login_model.im_login_infomation.status;      
        param_table["data"]["show_id"] = login_model.im_login_infomation.show_id;    
        param_table["data"]["uin"] = user_game_info.QQid;
        param_table["data"]["zone_name"] = talk_dialog_control.get_my_game_zone_info(26);

        base_utility.log(string.format("my info str = %s",base_utility.table_to_json(param_table)));

       web_utility.lua_call_js("get_my_info",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);

        user_game_info:delete();
    end;

    on_create_talk_session = function(groups, friends)
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        local group_table = {};
        local discussion_table = {};

        if groups ~= nil and #groups > 0 then
            for group_index = 1, #groups do
                if "Public" == groups[group_index].group_type then
                    table.insert(group_table, groups[group_index]);
                elseif "MobileDiscus" == groups[group_index].group_type then
                    table.insert(discussion_table, groups[group_index]);
                end
            end
            if _G.next(group_table) then
                param_table["data"]["groups"] = group_table;
            end
            if _G.next(discussion_table) then
                param_table["data"]["discussion"] = discussion_table;
            end
        end

        if friends ~= nil and #friends > 0 then
            param_table["data"]["friends"] = friends;
        end


        if groups ~= nil then
            for group_index = 1, #groups do
                if talk_dialog_control.is_group_session_opened(groups[group_index].id) == false then
                    if "Public" == groups[group_index].group_type then
                        table.insert(talk_dialog_control.current_session_group, groups[group_index]);
                    elseif "MobileDiscus" == groups[group_index].group_type then
                        table.insert(talk_dialog_control.current_session_group, groups[group_index]);
                    end
                end
            end
        end

        if friends ~= nil then
            for friend_index = 1, #friends do
                if talk_dialog_control.is_friend_session_opened(friends[friend_index].id) == false then
                    table.insert(talk_dialog_control.current_session_friend, friends[friend_index]);
                end
            end
        end 

        base_utility.log(string.format("[talk_dialog_control]create_talk_session, json_str = %s",base_utility.table_to_json(param_table)));
        web_utility.lua_call_js("create_talk_session",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        
        -- 设置关闭按钮的颜色
        if #talk_dialog_control.current_session_group + #talk_dialog_control.current_session_friend > 1 then
            talk_dialog_view:set_close_background(false);
        else
            talk_dialog_view:set_close_background(true);
        end
    end; 

    on_get_group_all_message = function(group_id)
        local history_unread_msg_count = group_list_model.get_group_history_unread_msg_count_remotely(group_id);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");

        local messages = group_message_model.get_group_messages(group_id);
        
        if group_list_model.get_has_ever_got_msg(group_id) == false then
            local  msg_count = 60;
            if history_unread_msg_count > 60 then
                msg_count = history_unread_msg_count;
            end

            social_network:get_group_msgs(group_id, msg_count);
        else
            local param_table = {};
            
            param_table["retCode"] = 0;
            param_table["msg"] = "success";
            param_table["group_id"] = group_id;
            param_table["data"] = messages;
            web_utility.lua_call_js("get_group_all_message",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
            base_utility.log(string.format("[talk_dialog_control]on_get_group_all_message, str = %s",base_utility.table_to_json(param_table)));
            talk_dialog_control.save_history_msg(group_id, messages);
        end


        group_list_model.clear_history_unread_msg_count(group_id); 
        group_list_model.clear_history_unread_msg_count_remotely(group_id);
        group_message_model.clear_group_messages(group_id);

        local table_param = {};
        table_param["group_id"] = group_id;
        event_center.send_event(group_event.GROUP_CLEAR_MESSAGE,base_utility.table_to_json(table_param));

        -- 设置上一个活动会话为已读
        if talk_dialog_control.active_session_id ~= nil and talk_dialog_control.active_session_flag ~= 0 then
            if talk_dialog_control.active_session_flag ~= talk_dialog_control.GROUP_FLAG or talk_dialog_control.active_session_id ~= group_id then
                local social_network =
                    base_utility.get_component_adapter("ISocial_network",
                        "ierd_tgp.social_network.Get_social_network_adapter();");
                social_network:set_msg_read(talk_dialog_control.active_session_flag, talk_dialog_control.active_session_id);
            end
        end

        -- 更改活动会话
        talk_dialog_control.active_session_id = group_id;
        talk_dialog_control.active_session_flag = talk_dialog_control.GROUP_FLAG;

        -- 设置当前的活动会话为已读
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:set_msg_read(talk_dialog_control.active_session_flag, talk_dialog_control.active_session_id);
        event_center.send_event(social_event.TALK_ACTIV_SESSION_CHANGED, base_utility.table_to_json({id = group_id, unread_count = history_unread_msg_count}));
    end;

    on_get_group_unread_message_count = function(group_id)
        local unread_message_count = group_list_model.get_group_history_unread_msg_count(group_id);

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["group_id"] = group_id;
        param_table["data"] = {};
        param_table["data"]["count"] = unread_message_count;
        
       web_utility.lua_call_js("get_group_unread_message_count",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    on_get_group_unread_message = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_get_group_unread_message, str = %s", json_str));

        local param_in = base_utility.json_to_table(json_str);
        local messages = param_in.groups;

        if messages == nil or param_in.group_id == nil then 
            --返回之前先设置下，没有历史消息的群组有bug
            --bug:http://tapd.oa.com/10041181/bugtrace/bugs/view?bug_id=1110041181052254718           
            if param_in.group_id ~= nil then
                group_list_model.set_has_ever_got_msg(param_in.group_id, true);
            end
            return;
        end

        if group_list_model.get_has_ever_got_msg(param_in.group_id) == true then
            base_utility.log(string.format("[talk_dialog_control]group_list_model.get_has_ever_got_msg(group_id) == true"));
            return talk_dialog_control.on_get_group_pre_page_message(event_id, json_str);
        end

        group_list_model.set_has_ever_got_msg(param_in.group_id, true);

        if param_in.result ~= 0 then
            return;
        end

        local messages_new_member = group_message_model.get_group_new_member_messages(param_in.group_id);
        group_message_model.clear_group_new_member_messages(group_id);

        if messages == nil or #messages == 0 then
            messages = messages_new_member;
        elseif messages_new_member ~= nil then
            for messages_new_member_i = 1, #messages_new_member do
                local mesage_new_member = messages_new_member[messages_new_member_i];
                talk_dialog_control.insert_msg_order(messages, mesage_new_member);
            end            
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["group_id"] = param_in.group_id;
        param_table["data"] = messages;
        web_utility.lua_call_js("get_group_all_message",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        live_room_control.on_get_group_unread_message(param_in.group_id, param_table)
        talk_dialog_control.save_history_msg(param_in.group_id, messages);
    end;


    on_get_group_pre_page_message = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_get_group_pre_page_message, str = %s", json_str));

        local param_in = base_utility.json_to_table(json_str);
        local messages = param_in.groups;

        if param_in.result ~= 0 then
            return;
        end

        if messages == nil or param_in.group_id == nil then
            return;
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["group_id"] = param_in.group_id;
        param_table["data"] = messages;
       web_utility.lua_call_js("get_group_pre_page_message",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        talk_dialog_control.save_history_msg(param_in.group_id, messages);
    end;

    on_new_group_message_come = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_new_group_message_come, str = %s", json_str));
        local message = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["group_id"] = message.group_id;
        local count = group_list_model.get_group_history_unread_msg_count(message.group_id);
        param_table["data"]["count"] = count;
        web_utility.lua_call_js("new_group_message_come",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        live_room_control.on_new_group_message_come(message.group_id, param_table);
    end;

    on_quit_group = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_quit_group, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local message = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table.group_id;
       web_utility.lua_call_js("quit_group",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    on_delete_group = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_delete_group, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table.group_id;
       web_utility.lua_call_js("delete_group",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);  
    end;

    on_broadcast_delete_group = function (data_in)
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["group_id"] = data_in.group_id;
        web_utility.lua_call_js("delete_group",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);  
    end;

    on_broadcast_del_friend = function (data_in)
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["friend_id"] = data_in.friend_id;
       web_utility.lua_call_js("delete_friend",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    put_my_give_game_msg_to_history = function(msg_str, show_in_web)
        base_utility.log("[talk_dialog_control] put_my_give_game_msg_to_history");
        talk_dialog_control.on_send_msg_to_friend(nil, msg_str);

        if show_in_web then
            local result_table = base_utility.json_to_table(msg_str);
            local param_table = {};
            param_table["retCode"] = result_table["result"];
            param_table["msg"] = "success";
            param_table["data"] = {};
            param_table["data"]["friend_id"] = result_table["friend_id"];
            param_table["data"]["receiver"] = result_table["receiver"];
            param_table["data"]["time"] = result_table["time"];
            param_table["data"]["time_custom"] = result_table["time_custom"];
            param_table["data"]["message"] = result_table["message"];
            web_utility.lua_call_js("put_my_give_game_msg_to_history",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 
        end
    end;

    on_send_msg_to_friend = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_send_msg_to_friend, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table["result"];
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["friend_id"] = result_table["friend_id"];
        param_table["data"]["receiver"] = result_table["receiver"];

        local is_my_friend = 0;
        if friend_list_model.get_friend_info(param_table["data"]["receiver"]) ~= nil then
            is_my_friend = 1;
        end 

        if is_my_friend == 1 then
            local message = base_utility.json_to_table(result_table.message);
            if message ~= nil and message.type ~= nil and message.type == social_utility.MSG_TYPE_LOL_KAIHEI_INVITE then 
                --开黑邀请信息
                param_table["data"]["message"] = result_table["message"];
               web_utility.lua_call_js("invite_lol_kaihei",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
                base_utility.log("send_msg_to_friend receive kaihei invite rsp msg:"..json_str, true);                
            else
               web_utility.lua_call_js("send_msg_to_friend",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 
            end
            
            if result_table.result == 0 then
                friend_list_control.on_send_msg_to_friend(result_table);
                local messages = talk_dialog_control.session_history_msg[result_table.friend_id];
                if(messages == nil) then
                    talk_dialog_control.session_history_msg[result_table.friend_id] = {};
                    messages = talk_dialog_control.session_history_msg[result_table.friend_id];
                end
                talk_dialog_control.insert_msg_order(messages, result_table);
            end
        else
            base_utility.log("send_msg_to_friend, receive is not my friend:"..json_str, true);
        end
    end;

    on_new_friend_message_come = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_new_friend_message_come, str = %s", json_str));
        local message = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["friend_id"] = message.friend_id;
        local count = friend_list_model.get_history_unread_msg_count(message.friend_id);
        param_table["data"]["count"] = count;
       web_utility.lua_call_js("new_friend_message_come",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    on_get_friend_all_message = function(friend_id)
        local history_unread_msg_count = friend_list_model.get_history_unread_msg_count_remotely(friend_id);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");

        local messages = friend_message_model.get_friend_messages(friend_id);

        if friend_list_model.get_has_ever_got_msg(friend_id) == false then
            local  msg_count = 60;
            if history_unread_msg_count > 60 then
                msg_count = history_unread_msg_count;
            end

            social_network:get_friend_msgs(friend_id, msg_count); 
        else
            local param_table = {};

            param_table["retCode"] = 0;
            param_table["msg"] = "success";
            param_table["friend_id"] = friend_id;
            param_table["data"] = messages;
           web_utility.lua_call_js("get_friend_all_message",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
            talk_dialog_control.save_history_msg(friend_id, messages);
        end

        friend_list_model.clear_history_unread_msg_count(friend_id); 
        friend_list_model.clear_history_unread_msg_count_remotely(friend_id); 
        friend_message_model.clear_friend_messages(friend_id);

        local table_param = {};
        table_param["friend_id"] = friend_id;
        event_center.send_event(group_event.FRIEND_CLEAR_MESSAGE,base_utility.table_to_json(table_param));  

        if talk_dialog_control.active_session_id ~= nil and talk_dialog_control.active_session_flag ~= 0 then
            if talk_dialog_control.active_session_flag ~= talk_dialog_control.FRIEND_FLAG or talk_dialog_control.active_session_id ~= friend_id then
                local social_network =
                    base_utility.get_component_adapter("ISocial_network",
                        "ierd_tgp.social_network.Get_social_network_adapter();");
                social_network:set_msg_read(talk_dialog_control.active_session_flag, talk_dialog_control.active_session_id);
            end
        end

        talk_dialog_control.active_session_id = friend_id;
        talk_dialog_control.active_session_flag = talk_dialog_control.FRIEND_FLAG;     

        event_center.send_event(social_event.TALK_ACTIV_SESSION_CHANGED, base_utility.table_to_json({id = friend_id, unread_count = history_unread_msg_count}));
    end;

    on_get_friend_unread_message = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_get_friend_unread_message, str = %s", json_str));

        local param_in = base_utility.json_to_table(json_str);
        local messages = param_in.friends;

        if messages == nil or param_in.friend_id == nil then
            --返回之前先设置下，没有历史消息的群组有bug
            --bug:http://tapd.oa.com/10041181/bugtrace/bugs/view?bug_id=1110041181052254718           
            if param_in.friend_id ~= nil then
                friend_list_model.set_has_ever_got_msg(param_in.friend_id, true);
            end
            return;
        end

        if friend_list_model.get_has_ever_got_msg(param_in.friend_id) == true then
            base_utility.log(string.format("[talk_dialog_control]friend_list_model.get_has_ever_got_msg(friend_id) == true"));
            return talk_dialog_control.on_get_friend_pre_page_message(event_id, json_str);
        end

        friend_list_model.set_has_ever_got_msg(param_in.friend_id, true);
        
        if param_in.result ~= 0 then
            return;
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["friend_id"] = param_in.friend_id;
        param_table["data"] = messages;
       web_utility.lua_call_js("get_friend_all_message",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        talk_dialog_control.save_history_msg(param_in.friend_id, messages);
    end;

    on_get_friend_pre_page_message = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_get_friend_pre_page_message, str = %s", json_str));

        local param_in = base_utility.json_to_table(json_str);
        local messages = param_in.friends;

        if messages == nil or param_in.friend_id == nil then
            return;
        end
        
        if param_in.result ~= 0 then
            return;
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["friend_id"] = param_in.friend_id;
        param_table["data"] = messages;
       web_utility.lua_call_js("get_friend_pre_page_message",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        talk_dialog_control.save_history_msg(param_in.friend_id, messages);
    end;

    on_get_friend_unread_message_count = function(friend_id)
        local unread_message_count = friend_list_model.get_history_unread_msg_count(friend_id);
        local  param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["friend_id"] = friend_id;
        param_table["data"] = {};
        param_table["data"]["count"] = unread_message_count;
        
       web_utility.lua_call_js("get_friend_unread_message_count",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);            
    end;

    on_get_friend_info  = function(friend_id)
        local friend_info = friend_list_model.get_friend_info(friend_id);
        local param_table = {};

        if friend_info == nil then 
            param_table["retCode"] = 1;
            param_table["msg"] = "fail";
            param_table["data"] = {};
           web_utility.lua_call_js("get_friend_info",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
            return;
        end

        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["face_url"] = friend_info["url"];
        param_table["data"]["friend_id"] = friend_info["friend_id"];
        param_table["data"]["name"] = friend_info["name"];
        param_table["data"]["new_name"] = friend_info["new_name"] or "";
        param_table["data"]["remark"] = friend_info["remark"];
        param_table["data"]["sex"] = friend_info["sex"];
        param_table["data"]["level"] = friend_info["level"];
        param_table["data"]["exp"] = friend_info["exp"];
        param_table["data"]["reg_time"] = friend_info["reg_time"];
        param_table["data"]["last_login_time"] = friend_info["last_login_time"];
        param_table["data"]["status"] = friend_info["status"];
        if friend_info["show_id"] ~= nil then
            param_table["data"]["show_id"] = friend_info["show_id"];
        end

        param_table["data"]["playing_game"] = {};
        if friend_info.playing_game ~= nil then
            param_table["data"]["playing_game"] = friend_info.playing_game;
        end

       web_utility.lua_call_js("get_friend_info",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;   

    on_delete_friend = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_delete_friend, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["friend_id"] = result_table.friend_id;
       web_utility.lua_call_js("delete_friend",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);  
    end; 

    on_get_group_member = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_get_group_member, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table.group_id;
        param_table["data"]["members"] = result_table.friends;
       web_utility.lua_call_js("get_group_member",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);        
    end;

    on_get_user_list_info = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_get_user_list_info, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["user_list_info"] = result_table.friends;
       web_utility.lua_call_js("get_user_list_info",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 

--[[
        if result_table.friends ~= nill then
            for i = 1, #result_table.friends do
                if result_table.friends[i].friend_id == login_model.im_login_infomation.friend_id then
                    login_model.im_login_infomation = result_table.friends[i];
                    break;
                end
            end    
        end]]
    end;


    on_close_sessions = function(json)
        local session_table = base_utility.json_to_table(json);
        if session_table == nil then
            return;
        end

        local friends = session_table.friends;
        local groups = session_table.groups;

        if friends ~= nil then
            for i = 1, #friends do 
                for friend_session_i = #talk_dialog_control.current_session_friend, 1, -1 do
                    if talk_dialog_control.current_session_friend[friend_session_i].id == friends[i] then
                        table.remove(talk_dialog_control.current_session_friend, friend_session_i);
                    end
                end
            end
        end

        if groups ~= nil then
            for i = 1, #groups do 
                for group_session_i = #talk_dialog_control.current_session_group, 1, -1 do
                    if talk_dialog_control.current_session_group[group_session_i].id == groups[i] then
                        table.remove(talk_dialog_control.current_session_group, group_session_i);
                    end
                end 
            end
        end 

        event_center.send_event(social_event.TALK_ACTIV_SESSION_CHANGED, base_utility.table_to_json({id = "", unread_count = 0}));  
        -- 设置关闭按钮的颜色
        if #talk_dialog_control.current_session_group + #talk_dialog_control.current_session_friend > 1 then
            talk_dialog_view:set_close_background(false);
        else
            talk_dialog_view:set_close_background(true);
        end     
    end;

    update_group_mask_msg_flag = function(group_id, flag)
        base_utility.log(string.format("[talk_dialog_control]update_group_mask_msg_flag"));
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["group_id"] = group_id;
        param_table["data"]["flag"] = flag;
       web_utility.lua_call_js("update_group_mask_msg_flag",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 
    end;

    get_my_game_zone_info = function(game_id)
        local servers = banner_model.get_game_servers(game_id);
        if servers == nil then
            base_utility.log(string.format("get_my_game_zone_info, servers is NIL"));
            return nil;
        end

        base_utility.log(string.format("get_my_game_zone_info, count:%d, selected:%d",
                      servers.server_count, servers.selleted));
        
        local selected_server = nil;
        for i= 0, servers.server_count - 1 do
            local server_info = servers.servers[i];
            if server_info ~= nil then
                if server_info.id == servers.selleted then
                    base_utility.log(string.format("get_my_game_zone_info, set cursel to %d(%d)", i, servers.selleted));
                    selected_server = server_info;
                    break;
                end
            else
                base_utility.log(string.format("refresh_servers, servers.servers[%d] == nil", i), true);
            end
        end

        if selected_server == nil then
            base_utility.log(string.format("get_my_game_zone_info, selected_server is NIL"));
            return nil;
        end

        local param_table = {};

        return selected_server.name;
        --[[
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["zone_id"] = selected_server.id;
        param_table["data"]["name"] = selected_server.name;
        param_table["data"]["state"] = selected_server.state;
        param_table["data"]["need_update"] = selected_server.need_update;
       web_utility.lua_call_js("get_my_game_zone_info",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); ]]
    end;

    on_open_lol_room = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_open_lol_room, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table.group_id;
        param_table["data"]["room_id"] = result_table.room_id;
       web_utility.lua_call_js("open_lol_room",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end; 

--[[
    on_get_lol_room_list = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_get_lol_room_list, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["room_list"] = result_table.groups;
       web_utility.lua_call_js("get_lol_room_list",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);        
    end; ]]  

    on_join_lol_chatroom = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_join_lol_chatroom, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table.group_id;
        param_table["data"]["chat_room"] = result_table.chat_room;
       web_utility.lua_call_js("join_lol_chatroom",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);        
    end;   

    on_update_lol_room = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_open_lol_room, str = %s", json_str));
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table.group_id;
        param_table["data"]["chat_room"] = result_table.chat_room;
       web_utility.lua_call_js("update_lol_room",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);        
    end; 

    on_delete_friend_other = function(friend_id)
        base_utility.log(string.format("[talk_dialog_control]on_delete_friend_other, friend_id = %s", friend_id));
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["friend_id"] = friend_id;
       web_utility.lua_call_js("delete_friend_other",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end; 

    on_delete_group_other = function(group_id)
        base_utility.log(string.format("[talk_dialog_control]on_delete_group_other, group_id = %s", group_id));
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["group_id"] = group_id;
       web_utility.lua_call_js("delete_group_other",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);        
    end; 

    flash_window_group = function(group_id)
        for group_session_i = 1, #talk_dialog_control.current_session_group do
            if talk_dialog_control.current_session_group[group_session_i].id == group_id then
                talk_dialog_view:flash_window();
            end
        end         
    end;

    flash_window_friend = function(friend_id)
        for friend_session_i = 1, #talk_dialog_control.current_session_friend do
            if talk_dialog_control.current_session_friend[friend_session_i].id == friend_id then
                talk_dialog_view:flash_window();
            end
        end
    end;

    on_foreground_window_changed = function(event_id, json_str)
        local param_table = base_utility.json_to_table(json_str);
        if talk_dialog_view:is_same_window(param_table.foreground_wnd) == 1 then
            is_window_active = true;
            return;
        end

        if is_window_active == false then
            return;
        end

        is_window_active = false;

        base_utility.log(string.format("[talk_dialog_control]talk_dialog_control.current_session_group=%s",base_utility.table_to_json(talk_dialog_control.current_session_group)));
        base_utility.log(string.format("[talk_dialog_control]talk_dialog_control.current_session_friend=%s",base_utility.table_to_json(talk_dialog_control.current_session_friend)));

        local should_flash = false;
        for group_session_i = 1, #talk_dialog_control.current_session_group do
            local group_id = talk_dialog_control.current_session_group[group_session_i].id;
            local history_unread_msg_count = group_list_model.get_group_history_unread_msg_count_remotely(group_id);
            base_utility.log(string.format("[talk_dialog_control]group_id=%s, history_unread_msg_count=%d", group_id, history_unread_msg_count));

            if history_unread_msg_count > 0 then
                should_flash = true;
                break;
            end
        end    

        for friend_session_i = 1, #talk_dialog_control.current_session_friend do
            local friend_id = talk_dialog_control.current_session_friend[friend_session_i].id;
            local history_unread_msg_count = friend_list_model.get_history_unread_msg_count_remotely(friend_id);
            base_utility.log(string.format("[talk_dialog_control]friend_id=%s, history_unread_msg_count=%d", friend_id, history_unread_msg_count));
            if history_unread_msg_count > 0 then
                should_flash = true;
                break;
            end
        end

        if should_flash == true then
            talk_dialog_view:flash_window();
        end
    end;

    on_restore = function()
        if talk_dialog_control.ignore_restore then
            return;
        end
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
       web_utility.lua_call_js("go_to_last_session_has_new_message",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 
    end;
    
    on_close_dailog_timer = function()
        base_utility.remove_interval("talk_dialog_control.on_close_dailog_timer");
        talk_dialog_view:close(); 
        --清理资源：
        talk_dialog_control.on_dialog_close_web();
        talk_dialog_control.init_ = false;
    end;

    -- 点击关闭按钮
    on_click_close_btn = function()
        -- 如果web加载成功了，则隐藏，否则直接销毁
        if talk_dialog_control.inited == true then
            -- 等待页面的close回调方法再清理资源
            local param_table = {};
            param_table["retCode"] = 0;
            param_table["data"] = {};
            web_utility.lua_call_js("dialog_close",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
            -- 启动一个超时定时器
            base_utility.add_interval("talk_dialog_control.on_close_dailog_timer", 5 * 1000); 
        else
            talk_dialog_view:close(); 
            --清理资源：
            talk_dialog_control.on_dialog_close_web();
            talk_dialog_control.init_ = false;
        end
    end;

    on_dialog_close_web = function()
        if talk_dialog_control.active_session_id ~= nil and talk_dialog_control.active_session_flag ~= 0 then
            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social_network:set_msg_read(talk_dialog_control.active_session_flag, talk_dialog_control.active_session_id);
        end
        -- 取消查询用户列表的游戏状态
        local param_tbl = {
            tgp_id_list = {};
        }
        talk_dialog_control.set_lol_gaming_user_list(base_utility.table_to_json(param_tbl));

        talk_dialog_control.init_func();
        event_center.send_event(social_event.TALK_ACTIV_SESSION_CHANGED, base_utility.table_to_json({id = "", unread_count = 0}));
    end;
    -- 处理外部的关闭，（比如alt+f4）
    on_dialog_close = function(event_id, json_str)
        if talk_dialog_control.active_session_id ~= nil and talk_dialog_control.active_session_flag ~= 0 then
            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social_network:set_msg_read(talk_dialog_control.active_session_flag, talk_dialog_control.active_session_id);
        end

        if #talk_dialog_control.current_session_group + #talk_dialog_control.current_session_friend > 1 then
            talk_dialog_view:show(true);
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        web_utility.lua_call_js("dialog_close",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 

        return true;     -- 不关闭
    end;

    on_dialog_close_exit_tgp = function(event_id, json_str)
        if talk_dialog_control.active_session_id ~= nil and talk_dialog_control.active_session_flag ~= 0 then
            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social_network:set_msg_read(talk_dialog_control.active_session_flag, talk_dialog_control.active_session_id);
        end

        if #talk_dialog_control.current_session_group + #talk_dialog_control.current_session_friend > 1 then
            talk_dialog_view:show(true);
        end

        --talk_dialog_control.init_func();

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["exit_tgp"] = 1;
       web_utility.lua_call_js("dialog_close",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 

        return true;
    end;

    on_get_game_list = function(event_id, json_str)
        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["uid"] = result_table.uid;
        param_table["count"] = result_table.count;
        param_table["data"] = {};
        param_table["data"]["game_list"] = result_table.game_list;
       web_utility.lua_call_js("get_game_list",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);        
    end;

    on_group_info_updated = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]group_info_updated, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["group_info"] = result_table;

       web_utility.lua_call_js("group_info_updated",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);  
    end;

    -------------------------------------------------------------------------  begin
    -- 从保存文件中获取历史Pk信息
    get_history_pk_result = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_NAMEPLATE_PK_HISTORY_RESULT, "talk_dialog_control.on_get_history_pk_result");
        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social:get_nameplate_pk_history_result();
    end;

    on_get_history_pk_result = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_NAMEPLATE_PK_HISTORY_RESULT, "talk_dialog_control.on_get_history_pk_result");
        
        talk_dialog_control.is_get_nameplate_pk_history = true;
        local param_tbl = base_utility.json_to_table(json_str); 
        if type(param_tbl["pk_result_list"]) == "table" and next(param_tbl["pk_result_list"]) then 
            local pk_result_list = {};    
            for key, var in ipairs(param_tbl["pk_result_list"]) do
                local item = base_utility.json_to_table(var["remark"]);
                table.insert(pk_result_list, item);
            end                   
            talk_dialog_model.save_history_pk_detail(pk_result_list);
        end
    end;

    -- 退出时保存未显示过的PK结果
    save_unread_pk_result_to_history_file = function(event_id, json_str)
        if talk_dialog_control.is_get_nameplate_pk_history == true then
            local unread_pk_result_list = talk_dialog_model.get_all_unread_pk_result();
            local will_save_list = {};
            will_save_list["list"] =  unread_pk_result_list;
            local social =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social:save_nameplate_pk_result(base_utility.table_to_json(will_save_list));
        end
    end;

    -- group 聊天 seesion 打开时web主动从lua获取是否有Pk消息
    have_pk_result = function(group_id)  
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["group_id"] = group_id;
        param_table["data"]["have_pk_result"] = talk_dialog_model.get_unread_pk_result_count(group_id);
        param_table["data"]["list"] = {};
        param_table["data"]["count"] = 0;  

        local result_list = talk_dialog_model.get_unread_pk_result_list(group_id);
        if type(result_list) == "table" and next(result_list) then         
            param_table["data"]["count"] = #result_list;     
            param_table["data"]["list"] = result_list;
        end
       web_utility.lua_call_js("have_pk_result",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 
    end;

    -- web点击群聊小红点获取pk消息
    get_group_pk_result_detail = function(group_id)
        local result_list = talk_dialog_model.get_unread_pk_result_list(group_id);

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["group_id"] = group_id;   
        param_table["data"]["count"] = 0;     
        param_table["data"]["list"] = {};
        if type(result_list) == "table" and next(result_list) then         
            param_table["data"]["count"] = #result_list;     
            param_table["data"]["list"] = result_list;
        end
        base_utility.log("[get_group_pk_result_detail]:"..base_utility.table_to_json(param_table), true);
       web_utility.lua_call_js("get_group_pk_result_detail",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP); 
        talk_dialog_model.clear_unread_pk_result_list(group_id);
    end;

    -- 处理抢铭牌结果
    -- 被动方才会通知、保存小红点PK结果      
    -- e.g: A抢B的铭牌，如果A赢了，会推送一条全员消息
    -- 如果B赢了，只会推送给A一条防守成功的消息
    -- 直接获得铭牌也会有一条消息
    process_group_pk_result = function(result_table)
        local group_id = result_table.group_id;--之前是我的铭牌id，说明是被动的 现在发生了变化 
        local pk_result_item = base_utility.json_to_table(result_table.remark);
        -- before_pk_owner: 铭牌在PK之前的归属
        if tonumber(pk_result_item.before_pk_owner) == tonumber(login_model.im_login_infomation.friend_id) then
            base_utility.log("[process_group_pk_result] group_id: "..tostring(group_id)..", content:"..tostring(base_utility.table_to_json(pk_result_item)), true);
            --保存
            pk_result_item["group_id"] = group_id;
            talk_dialog_model.save_group_pk_detail(group_id, pk_result_item);

            if talk_dialog_control.is_group_session_opened(group_id) == true then
                local param_table = {};
                param_table["retCode"] = 0;
                param_table["data"] = {};
                param_table["data"]["group_id"] = group_id;
                param_table["data"]["nameplate_msg"] = pk_result_item;
               web_utility.lua_call_js("show_pk_result_reddot",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);    
                base_utility.log("[process_group_pk_result] group_id: "..tostring(group_id).." session opened", true);
            end  
        end   
    end;

    process_nameplate_message = function(result_table)
        local group_id = result_table.group_id;
        local pk_result_item = base_utility.json_to_table(result_table.remark);
        
        pk_result_item["group_id"] = group_id;
        talk_dialog_model.save_group_pk_detail(group_id, pk_result_item);

        if talk_dialog_control.is_group_session_opened(group_id) == true then
            local param_table = {};
            param_table["retCode"] = 0;
            param_table["data"] = {};
            param_table["data"]["group_id"] = group_id;
            param_table["data"]["nameplate_msg"] = pk_result_item;
           web_utility.lua_call_js("show_pk_result_reddot",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);    
            base_utility.log("[process_nameplate_message] group_id: "..tostring(group_id).." session opened", true);
        end  
    end;
    -------------------------------------------------------------------------   end
    on_recv_group_report = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_recv_group_report, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);

        -- 抢铭牌结果
        if result_table.type == talk_dialog_control.NEW_GROUP_SYS_REPORT then
            local remark_tbl = base_utility.json_to_table(result_table.remark);
            if remark_tbl["type"] == "nameplate_pk" then
                talk_dialog_control.process_group_pk_result(result_table);
            elseif remark_tbl["type"] == "nameplate" then --名牌相关以后使用统一的消息
                talk_dialog_control.process_nameplate_message(result_table);
            end
        end

        if result_table.type == talk_dialog_control.NEW_GROUP_NEW_MEMBER_MSG_TYPE then
            local new_msg = {}; 
            new_msg.group_id = result_table.group_id;
            new_msg.sender = result_table.sender;
            new_msg.time = result_table.time;
            new_msg.msg_type = 1;
            new_msg.action_type = talk_dialog_control.NEW_GROUP_NEW_MEMBER_MSG_TYPE;

            
            local messages = group_message_model.get_group_messages(new_msg.group_id);
            talk_dialog_control.insert_msg_order(messages, new_msg);
            
            --group_message_model.add_message(new_msg);
            group_message_model.add_new_member_message(new_msg);
            --[[
            local messages = {};
            table.insert(messages, new_msg);
            talk_dialog_control.save_history_msg(new_msg.group_id, messages);]]
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["group_report"] = result_table;

        web_utility.lua_call_js("recv_group_report",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);  
        live_room_control.on_recv_group_report(result_table.group_id, param_table);
    end;

----------------------------------------------------------------------------- end
     on_recv_friend_report = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_recv_friend_report, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["friend_report"] = result_table;

       web_utility.lua_call_js("recv_friend_report",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);  
    end;

    on_broadcast_update_friend_online_status = function(data_in)
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["friend_report"] = {};
        param_table["data"]["friend_report"]["szRemark"] = base_utility.table_to_json(data_in);

       web_utility.lua_call_js("recv_friend_report",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

--[[
    get_new_group_member_msg = function(group_id)
        local messages = group_message_model.get_group_new_member_messages(group_id);
        group_message_model.clear_group_new_member_messages(group_id);

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["data"] = {};
        param_table["data"]["messages"] = messages;

       web_utility.lua_call_js("get_new_group_member_msg",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);          
    end;]]

    is_group_session_active = function(group_id)
        if talk_dialog_control.active_session_flag == talk_dialog_control.GROUP_FLAG and talk_dialog_control.active_session_id == group_id then
            return true;
        end

        return false;    
    end;

    is_friend_session_active = function(friend_id)
        if talk_dialog_control.active_session_flag == talk_dialog_control.FRIEND_FLAG and talk_dialog_control.active_session_id == friend_id then
            return true;
        end

        return false;    
    end;   

    is_group_session_opened = function(group_id)
        for session_i = 1, #talk_dialog_control.current_session_group do
            if talk_dialog_control.current_session_group[session_i].id == group_id then
                return true;
            end
        end

        return false;    
    end;

    is_friend_session_opened = function(friend_id)
        for session_i = 1, #talk_dialog_control.current_session_friend do
            if talk_dialog_control.current_session_friend[session_i].id == friend_id then
                return true;
            end
        end

        return false;    
    end;    

    on_self_info_updated = function()
        if login_model.im_login_infomation == nil then
            return;
        end

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["id"] = login_model.im_login_infomation.friend_id;
        param_table["data"]["my_nick"] = login_model.im_login_infomation.name;
        param_table["data"]["head_url"] = login_model.im_login_infomation.url;
        param_table["data"]["remark"] = login_model.im_login_infomation.remark;
        param_table["data"]["level"] = login_model.im_login_infomation.level;
        param_table["data"]["sex"] = login_model.im_login_infomation.sex;    
        param_table["data"]["exp"] = login_model.im_login_infomation.exp;
        param_table["data"]["reg_time"] = login_model.im_login_infomation.reg_time; 
        param_table["data"]["last_login_time"] = login_model.im_login_infomation.last_login_time;
        param_table["data"]["status"] = login_model.im_login_infomation.status;      
        param_table["data"]["show_id"] = login_model.im_login_infomation.show_id;    

        base_utility.log(string.format("on_self_info_updated str = %s",base_utility.table_to_json(param_table)));

       web_utility.lua_call_js("self_info_updated",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    save_history_msg = function(id, messages)
        if id == nil or messages == nil then
            return;
        end

        if talk_dialog_control.session_history_msg[id] == nil then
            talk_dialog_control.session_history_msg[id] = messages;
        else
            for i = 1, #messages do
                table.insert(talk_dialog_control.session_history_msg[id], messages[i]);
            end
        end
    end;



    history_max_cnt = 20;
    not_more_history_msg = {};

    get_history_message = function(data_in)
        local id = data_in.id;
        local pos = data_in.pos;
        local req_type = data_in.type;

        -- 最多维持100条 避免内存爆表
        if tonumber(pos) >= 100 then
            return {result = 0, id = id, message_list = {}};   
        end

        if talk_dialog_control.is_in_req_history_msg_queue(id) == true then
            --已经发起请求 等待结果
            return {result = 1, id = id, message_list = {}};   
        end

        local message_list = talk_dialog_control.get_history_message_imp(id, pos);
        if #message_list < talk_dialog_control.history_max_cnt then -- 不够20条
            if talk_dialog_control.not_more_history_msg[id] == true then --已经没有更多了
                return {result = 0, id = id, message_list = message_list};
            else
                -- 不足，而且可能还有，则调取接口获取
                talk_dialog_control.req_history_msg(id, req_type, pos);
                return {result = 1, id = id, message_list = {}};         
            end
        end

        return {result = 0, id = id, message_list = message_list};
    end;

    ----------------------------------
    history_msg_request_que = {};

    is_in_req_history_msg_queue = function(id)
        local ret = false;
        for i = 1, #talk_dialog_control.history_msg_request_que do
            if talk_dialog_control.history_msg_request_que[i].id == id then
                ret = true;
                break;
            end
        end
        return ret;
    end;

    req_history_msg = function(id, req_type, pos)
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        if req_type == "group" then
            social_network:get_group_history_msgs(id, talk_dialog_control.history_max_cnt);
        else
            social_network:get_friend_history_msgs(id, talk_dialog_control.history_max_cnt);
        end
        table.insert(talk_dialog_control.history_msg_request_que, {id = id, pos = pos});
    end;

    req_history_group_msg_rsp = function(event_id, json_str)
        local param = base_utility.json_to_table(json_str);
        base_utility.log("[talk_dialog_control]req_history_group_msg_rsp result:"..tostring(param.result)..
            ", group_id:"..param.group_id..", msg size:"..tostring(param.groups), true);

        if param.result == 0 then
            talk_dialog_control.save_history_msg(param.group_id, param.groups);
            if #param.groups < talk_dialog_control.history_max_cnt then
                talk_dialog_control.not_more_history_msg[param.group_id] = true;
            end
        else
            talk_dialog_control.not_more_history_msg[param.group_id] = true;
        end

        talk_dialog_control.process_history_msg_request(param.group_id);
    end;

    req_history_friend_msg_rsp = function(event_id, json_str)
        local param = base_utility.json_to_table(json_str);
        base_utility.log("[talk_dialog_control]req_history_friend_msg_rsp result:"..tostring(param.result)..
            ", friend_id:"..param.friend_id..", msg size:"..tostring(#param.friends), true);

        if param.result == 0 then
            talk_dialog_control.save_history_msg(param.friend_id, param.friends);
            if #param.friends < talk_dialog_control.history_max_cnt then
                talk_dialog_control.not_more_history_msg[param.friend_id] = true;
            end
        else
            talk_dialog_control.not_more_history_msg[param.friend_id] = true;
        end

        talk_dialog_control.process_history_msg_request(param.friend_id);
    end;

    process_history_msg_request = function(group_id)
        for i = 1, #talk_dialog_control.history_msg_request_que do
            if talk_dialog_control.history_msg_request_que[i].id == group_id then
                local message_list = talk_dialog_control.get_history_message_imp(group_id, talk_dialog_control.history_msg_request_que[i].pos);
                comm_center.send_broadcast('i_broadcast_social_on_get_history_message', {id = group_id, message_list = message_list});
                table.remove(talk_dialog_control.history_msg_request_que, i);
                break;
            end
        end
    end;

    get_history_message_imp = function(group_id, pos)
        local result_list = {};
        local messages = talk_dialog_control.session_history_msg[group_id];
        base_utility.log("[talk_dialog_control]get_history_message_imp group_id:"..group_id..
            ", pos:"..pos, true);
        if messages ~= nil then
            base_utility.log("[talk_dialog_control]get_history_message_imp group_id:"..group_id..
                ", pos:"..pos..", msg size:"..tostring(#messages), true);
        end
        if messages ~= nil and pos < #messages  then
            local loop_cnt = 0;
            if #messages - pos >= talk_dialog_control.history_max_cnt then
                loop_cnt = talk_dialog_control.history_max_cnt;
            else
                loop_cnt = #messages - pos;
            end
            for i = pos + 1, pos + loop_cnt do
                table.insert(result_list, messages[i]);
            end
        end
        return result_list;
    end;


    -- 废弃
    --get_history_msg = function(id, derection, offset, count)
       --  local messages = talk_dialog_control.session_history_msg[id];
       --  local messages_out = {};

       --  if derection == 0 then
       --      for index = offset, #messages do
       --          if #messages_out >= count then
       --              break;
       --          end

       --          table.insert(messages_out, messages[index]);
       --      end     
       --  else
       --      for index = offset, 1, -1 do
       --          if #messages_out >= count then
       --              break;
       --          end

       --          table.insert(messages_out, messages[index]);
       --      end                      
       --  end

       --  local param_table = {};
       --  param_table["retCode"] = 0;
       --  param_table["msg"] = "success";
       --  param_table["data"] = {};
       --  param_table["data"]["messages"] = messages_out;
       --  param_table["data"]["total_messages_count"] = #messages; 

       --  base_utility.log(string.format("get_history_msg str = %s",base_utility.table_to_json(param_table)));
       -- web_utility.lua_call_js("get_history_msg",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);    
    --end;


    insert_msg_order = function(messages, message)
        if messages == nil or #messages == 0 then
            messages = {};
            table.insert(messages, message);
            return;
        end

        for messages_i = 1, #messages do
            if message.time >= messages[messages_i].time then
                table.insert(messages, messages_i, message);
                return;
            end
        end
        
        table.insert(messages, message);    
    end;

    on_set_group_admin  = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_set_group_admin, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = {};
        param_table["data"]["group_id"] = result_table.group_id;
        param_table["data"]["application_id"] = result_table.application_id;

       web_utility.lua_call_js("set_group_admin",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);      
    end; 

    on_open_browse_img = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_open_browse_img, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = result_table

        web_utility.lua_call_js("open_img_browse_floder",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);    
        live_room_control.on_open_browse_img(result_table.id, param_table);  
    end;

    on_pic_upload_result  = function(event_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_pic_upload_result, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = result_table;

       web_utility.lua_call_js("pic_upload_result",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);    
    end;

    set_nameplate = function(group_id, nameplate_id)
        base_utility.log("[talk_dialog_control][set_nameplate]group_id:"..tostring(group_id)..", nameplate_id:"..tostring(nameplate_id));
        local social_network_model =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network_model:set_nameplate(group_id, nameplate_id);
    end;


    add_friend = function(friend_id)
        local ext_info = {};
        ext_info["type"] = 3 --关系:qq好友 游戏好友, 3-未知, 如果有最近游戏字段， 填写游戏id ext_info["game_id"] =， 没有则不填写

        local from = {};
        if login_model.im_login_infomation then --附加字段只有120个字符，这里都简写了
            from = social_network_control.get_my_remark_info();
        end
        ext_info["f"] = from;

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_ADD_FRIEND, "talk_dialog_control.add_friend_rsp");

        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:add_friend(friend_id,base_utility.table_to_json(ext_info));    
    end;

    add_friend_rsp = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_ADD_FRIEND, "talk_dialog_control.add_friend_rsp");
    end;

    show_talk_dialog = function(friend_id)
        local friends = {};
        local info = {};
        info.id = friend_id;
        info.is_new = false;
        info.time = friend_list_model.get_last_msg_time(info.id);
        table.insert(friends, info);
        local param = {};
        param["friends"] = friends;
        event_center.send_event(group_event.SHOW_TALK_DIALOG,base_utility.table_to_json(param)); 
    end;

    is_my_friend = function(friend_id)
        local is_my_friend = 0;
        if friend_list_model.get_friend_info(friend_id) ~= nil then
            is_my_friend = 1;
        end;

        local param_table = {};
        param_table.retCode = 0;
        param_table.data = {};
        param_table.data.is_my_friend = is_my_friend;
        param_table.data.friend_id = friend_id;
       web_utility.lua_call_js("is_my_friend",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);            
    end;



    --取消管理权限
    cancel_manager = function(group_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_SET_GROUP_ADMIN, "talk_dialog_control.cancel_manager_rsp");

        local param = base_utility.json_to_table(json_str);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:set_group_admin(0, param["group_id"], param["member_id"]);
    end;

    cancel_manager_rsp = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_SET_GROUP_ADMIN, "talk_dialog_control.cancel_manager_rsp");

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};

        local result_param = base_utility.json_to_table(json_str);
        param_table["data"]["member_id"] = result_param["application_id"];

        if result_param["result"] == 0 then
            param_table["data"]["manager"] = 0; --取消成功，变成非管理员
        else
            param_table["retCode"] = result_param["result"];
            param_table["msg"] = LoadStr("SOCIAL_TALK_DIALOG_CANCEL_MANAGER_FAIL");
            param_table["data"]["manager"] = 1;
            base_utility.log("[talk_dialog_control][cancel_manager_rsp] cancel_manager_rsp error code:"..tostring(result_param["result"]), true);
        end
       web_utility.lua_call_js("cancel_manager",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    --设置管理员权限
    set_manager = function(group_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_SET_GROUP_ADMIN, "talk_dialog_control.set_manager_rsp");

        local param = base_utility.json_to_table(json_str);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:set_group_admin(300, param["group_id"], param["member_id"]);
    end;

    set_manager_rsp = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_SET_GROUP_ADMIN, "talk_dialog_control.set_manager_rsp");

        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};

        local result_param = base_utility.json_to_table(json_str);
        param_table["data"]["member_id"] = result_param["application_id"];

        if result_param["result"] == 0 then
            param_table["data"]["manager"] = 1; --设置成功，变成管理员
        else
            param_table["retCode"] = result_param["result"];
            param_table["msg"] = LoadStr("SOCIAL_TALK_DIALOG_SET_MANAGER_FAIL");
            param_table["data"]["manager"] = 0;
            base_utility.log("[talk_dialog_control][set_manager_rsp] error code:"..tostring(result_param["result"]), true);
        end
       web_utility.lua_call_js("set_manager",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    --删除成员
    remove_member = function(group_id, json_str)
        local param = base_utility.json_to_table(json_str);
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_KICKOUT_GROUP_MEMBER, "talk_dialog_control.remove_member_rsp");

        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:kickout_group_member(param["group_id"], param["member_id"]);
    end;

    remove_member_rsp = function(event_id, json_str)
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_KICKOUT_GROUP_MEMBER, "talk_dialog_control.remove_member_rsp");

        local result_param = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {member_id = result_param["application_id"]};
        if result_param["result"] ~= 0 then
            param_table["retCode"] = result_param["result"];
            param_table["msg"] = LoadStr("SOCIAL_TALK_DIALOG_DELETE_MEMBER_FAIL");
            base_utility.log("[talk_dialog_control][remove_member_rsp] error code:"..tostring(result_param["result"]), true);
        end

       web_utility.lua_call_js("remove_member",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
    end;   


    on_set_clipboard_text_result  = function(group_id, json_str)
        base_utility.log(string.format("[talk_dialog_control]on_set_clipboard_text_result, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);
        local param_table = {};
        param_table["retCode"] = result_table.result;
        param_table["data"] = result_table;

       web_utility.lua_call_js("set_clipboard_text",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);   
    end;

    on_show = function(uilib, ctrl_id)
        base_utility.log("[talk_dialog_control] on_show talk_dialog_view tpf");
        talk_dialog_view:on_show(uilib, ctrl_id);
    end;

    invite_lol_kaihei = function(friend_id, nick_name)
        local invite_lol_kaihei_param = {};
        invite_lol_kaihei_param.friend_id = friend_id;
        invite_lol_kaihei_param.nick_name = nick_name;
        event_center.send_event(social_event.INVITE_LOL_KAIHIE,base_utility.table_to_json(invite_lol_kaihei_param));
    end;

    get_current_lol_zone_id = function(game_id, customer_id)
        local zone_id = social_network_model.get_lol_zone_id();
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {zone_id = zone_id, customer_id = customer_id};

       web_utility.lua_call_js("get_current_lol_zone_id",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);   
    end;


    on_tgp_st_renewed = function(event_id, json_str)
        base_utility.log("[talk_dialog_control] on_tgp_st_renewed");
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil then
            local param_table = {};
            param_table["retCode"] = 0;
            param_table["msg"] = "success";
            param_table["data"] = {
                st_key = param_tbl["st_key"], 
                st = param_tbl["st"], 
                uin = param_tbl["uin"],
                tgp_id = param_tbl["tgp_id"],
                tgp_ticket = param_tbl["web_token"],
                tgp_biz_ticket = param_tbl["biz_ticket"]
            };

            web_utility.lua_call_js("set_social_cookie",base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
            live_room_control.on_tgp_st_renewed(param_table);
        end
    end;

    is_session_exist = function(sessions, id)
        if sessions == nil or id == nil then
            return false;
        end

        for session_i = 1, #sessions do
            if sessions[session_i].id == id then
                return true;
            end
        end

        return false;    
    end;

    show_create_discussion_dialog = function(json_str)
        event_center.send_event(group_event.SHOW_CREATE_DISCUSSION_DIALOG, json_str);
    end;

    edit_discussion_name = function(json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        local old_group_info = group_list_model.get_group_info(param_tbl["group_id"]);
        if old_group_info ~= nil then
            local group_info = ierd_tgp.social_network.tagGroupInfo:new();
            group_info.szGroup_id     = param_tbl["group_id"];
            group_info.szGroup_name   = param_tbl["name"];
            group_info.szGroup_face_url = old_group_info["face_url"];
            local social =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social:modify_group_info(group_info);
            group_info:delete();
        else
            base_utility.log("[edite_discussion_name]get group_info_old error, id:"..tostring(param_tbl["group_id"]), true);
        end
    end;

    -- lol事件数据发送改变
    on_lol_event_data_changed = function(param_tbl)
        if nil == param_tbl then return end
        if talk_dialog_control.is_friend_session_active(tostring(param_tbl["tgp_id"])) then
            local param_table = {};
            param_table["retCode"] = 0;
            param_table["friend_id"] = param_tbl["tgp_id"];
            param_table["data"] = param_tbl["detail_info"];
            web_utility.lua_call_js("update_friend_lol_event_data", base_utility.table_to_json(param_table)
                ,CID_WEB_CTRL_FOR_TALK_GROUP);
        end
    end;

    update_lol_game_status_response = function(tbl_id_list)
        if tbl_id_list["update_status_list"] ~= nil and #tbl_id_list["update_status_list"] > 0 then
            for i, user_id in ipairs(tbl_id_list["update_status_list"]) do
                if talk_dialog_control.is_friend_session_active(user_id) then
                    local param_table = talk_dialog_control.get_valid_lol_status_data_helper(user_id);
                    web_utility.lua_call_js("update_friend_lol_event_data", base_utility.table_to_json(param_table)
                            ,CID_WEB_CTRL_FOR_TALK_GROUP);
                    break;   -- 如果找到了，则退出循环
                end
            end
        end
    end;

    get_friend_game_state = function(json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil then
            local param_table = talk_dialog_control.get_valid_lol_status_data_helper(param_tbl["friend_id"]);
            web_utility.lua_call_js("get_friend_game_state", base_utility.table_to_json(param_table)
                ,CID_WEB_CTRL_FOR_TALK_GROUP);
        end
    end;

    get_valid_lol_status_data_helper = function(tgp_id_param)
        if nil == tgp_id_param then return end
        -- 获取lol的事件数据
        local msg_param = { 
            tgp_id = tgp_id_param
        };
        local ret_lol_status_tbl = comm_center.call_service(
                "i_service_social_info_center_get_lol_player_status", msg_param);
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["friend_id"] = ret_lol_status_tbl["tgp_id"];
        -- 检查是否正在玩lol
        local ret_playing_lol = comm_center.call_service("i_service_lol_game_status_is_playing_lol_game", msg_param);
        if 1 == ret_playing_lol.is_playing_lol then 
            -- 校验数据是否有效
            local ret_valid_tbl = comm_center.call_service("i_service_lol_game_status_check_lol_status_valid", msg_param);
            if 1 == ret_valid_tbl.is_valid then
                param_table["data"] = ret_lol_status_tbl["detail_info"];
            else
                param_table["data"] = talk_dialog_control.modify_lol_user_status_data(ret_lol_status_tbl);
                base_utility.log("[talk_dialog_control] ret invalid data, overdue");
            end
        else
            -- 没有玩lol，直接返回-1
            param_table["data"] = talk_dialog_control.modify_lol_user_status_data(ret_lol_status_tbl);
            base_utility.log("[talk_dialog_control] not playing lol game.");
        end
        return param_table;
    end;

    set_lol_gaming_user_list = function(json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil and param_tbl.tgp_id_list ~= nil then
            local user_id_params = {
                user_list = param_tbl.tgp_id_list;
            };
            comm_center.call_service("i_service_social_info_center_set_lol_gaming_query_user_list", 
                user_id_params);
           event_center.send_event(social_event.SET_LOL_GAMING_USER_LIST, 
                base_utility.table_to_json(user_id_params));
        end
    end;

    get_game_names = function(json_str)
        local ret_tbl = {
            retCode = 0,
            data = {};
        };
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil and param_tbl.game_id ~= nil then
            for i, game_id_param in ipairs(param_tbl.game_id) do
                if game_id_param ~= 0 then
                    local game_info = banner_model.get_game_base_info(game_id_param);
                     if game_info ~= nil then
                         table.insert(ret_tbl.data, { game_id = game_id_param, game_name = game_info.name });
                     end
                end
            end
        end
        web_utility.lua_call_js("get_game_name", base_utility.table_to_json(ret_tbl)
                ,CID_WEB_CTRL_FOR_TALK_GROUP);
    end;

    handle_lol_user_status_exception = function(event_id, json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil and param_tbl.tgp_id ~= nil then
            if talk_dialog_control.is_friend_session_active(tostring(param_tbl["tgp_id"])) then
                local param_table = {};
                param_table["retCode"] = 0;
                param_table["friend_id"] = param_tbl["tgp_id"];
                
                local ret_tbl = comm_center.call_service("i_service_social_info_center_get_lol_player_status", param_tbl);
                param_table["data"] = talk_dialog_control.modify_lol_user_status_data(ret_tbl);
               
                web_utility.lua_call_js("update_friend_lol_event_data", base_utility.table_to_json(param_table)
                    ,CID_WEB_CTRL_FOR_TALK_GROUP);
            end
        end
    end;

    modify_lol_user_status_data = function(ret_status_tbl)
         local ret_json = "";
         if ret_status_tbl ~= nil and ret_status_tbl.detail_info ~= nil 
            and ret_status_tbl.detail_info ~= "" and ret_status_tbl.detail_info ~= "0" then
            local param = base_utility.json_to_table(ret_status_tbl.detail_info);
            if param ~= nil and "table" == type(param) and param.d ~= nil and "table" == type(param.d) then
                param.d[4] = -1;     -- 修改event_type为异常的-1
            end 
            ret_json = base_utility.table_to_json(param);
         end
         return ret_json;
    end;

    set_game_invitation_result = function(json_str)
        local rail_host = base_utility.get_component_adapter(
            "IRailHost", "ierd_tgp.tgp_plugin.GetRailHostAdapter();");
        if rail_host ~= nil then
            rail_host:SetGameInvitationResult(json_str);
        end
    end;

    get_game_base_info = function(json_str)
        local param = base_utility.json_to_table(json_str);
        if param ~= nil then
            local param_table = {};
            param_table["retCode"] = 0;
            param_table["msg"] = "success";
            param_table["data"] = {};
            param_table["data"]["friend_id"] = param["friend_id"];
            param_table["data"]["game_infos"] = {};

            for index, gid in pairs(param["game_ids"]) do
                local game_info = banner_model.get_game_base_info(gid);
                if game_info ~= nil then
                    param_table["data"]["game_infos"][tostring(gid)] = {game_name        = game_info.name, 
                                                                        mini_icon_url    = game_info.mini_icon_url, 
                                                                        icon_url         = game_info.icon_url,
                                                                        banner_icon_url  = game_info.banner_icon_url,
                                                                        hompage_icon_url = game_info.hompage_icon_url,
                                                                        taskbar_icon_url = game_info.taskbar_icon_url,
                                                                        logo_url         = "",
                                                                        poster_url = game_info.poster_url};
                else
                    base_utility.log("[talk_dialog_control][get_game_base_info] get game info error, id:"..tostring(gid), true);
                end
            end

            web_utility.lua_call_js("get_game_base_info", base_utility.table_to_json(param_table), CID_WEB_CTRL_FOR_TALK_GROUP);
        else
            base_utility.log("[talk_dialog_control][get_game_base_info] param error:"..json_str, true);
        end
    end;

    service_open_session = function (data_in)
        local data_out = {result=0, id=data_in.id, type=data_in.type};
        local table_param = {};

        local info = {};
        info.id = data_in.id;
        info.is_new = false;
        ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");

        if data_in.type == "Public" or  data_in.type == "MobileDiscus" then
            if group_list_model.get_group_info(info.id) == nil then
                data_out['result'] = 1;
                return data_out;
            end
            local groups = {};
            info.time = group_list_model.get_last_msg_time(info.id);
            info.group_type = group_list_model.get_group_type(info.id);
            table.insert(groups, info);
            table_param["groups"] = groups;
        elseif data_in.type == "Friend" then
            if friend_list_model.get_friend_info(info.id) == nil then
                data_out['result'] = 1;
                return data_out;
            end
            local friends = {};
            info.time = friend_list_model.get_last_msg_time(info.id);
            table.insert(friends, info);
            table_param["friends"] = friends;
        end

        event_center.send_event(group_event.SHOW_TALK_DIALOG,base_utility.table_to_json(table_param));

        return data_out;
    end;

    on_web_browser_crash = function()
        talk_dialog_control.inited = false;
    end;
};

talk_dialog_control.init();